package com.itfreer.gis.arcgis;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.file.IAnalyseFileService;
import com.itfreer.gis.OptionResult;
import com.itfreer.gis.cad.analysis.DxfAnalysis;
import com.itfreer.gis.entity.GisDataImportEntity;
import com.itfreer.gis.graphic.GisGraphic;
import com.itfreer.gis.service.GisDataImportService;
import com.itfreer.gis.shape.analysis.ShapeAnalysis;
import com.itfreer.power.entity.user.VUserInfoEntity;
import com.itfreer.utils.json.JsonUtils;
/**
 * shape文件导入arcgis数据库
 * @author gj
 *
 */
@Component("arcgisFileImport")
public class ArcgisFileImportServiceImp implements IAnalyseFileService {
	
	@Autowired
	GisDataImportService impService;

	@Override
	public String analyseFile(Map<String, String[]> param, String filePath,Object expnds) {
		
		
		try {
			String baseUrl=param.get("url")[0];
			Integer layerId=Integer.parseInt(param.get("layerId")[0]);
			String keyField=param.get("keyField")[0];
			String target=param.get("table")[0];
			List<GisGraphic> lst=null;
			String source="";
			if(filePath.endsWith(".zip")) {
				ShapeAnalysis analyse=new ShapeAnalysis();
				lst=analyse.analysis(filePath);
				source="shape文件";
			}else if(filePath.endsWith(".dxf")) {
				DxfAnalysis analyse=new DxfAnalysis();
				lst=analyse.analysis(filePath);
				source="CAD dxf文件";
			}
			
			if(lst!=null && lst.size()>0) {
				
				List<OptionResult> lstResult=new ArrayList<OptionResult>();
				
				boolean success=true;
				
				GisDataImportEntity entity=new GisDataImportEntity();
				entity.setId(UUID.randomUUID().toString().replace("-", ""));
				entity.setImportDate(new Date());
				entity.setSource(source);
				entity.setTarget(target);
				if(expnds!=null) {
					VUserInfoEntity user=(VUserInfoEntity)expnds;
					if(user!=null) {
						entity.setImportUserID(user.getUserid());
						entity.setImportUserName(user.getUserName());
					}
				}
				
				int su=0;
				int fa=0;
				
				for(GisGraphic graphic:lst) {
					//设置主键问题
					graphic.setKeyField(keyField);
					OptionResult iOption=new ArcgisOption().addFeature(baseUrl, layerId, graphic);
					lstResult.add(iOption);
					if(iOption.status) {
						if(entity.getSuccess()==null) {
							entity.setSuccess(1);
						}else {
							entity.setSuccess(entity.getSuccess()+1);
						}
						su++;
					}else {
						fa++;
						success=false;
						if(entity.getFail()==null) {
							entity.setFail(1);
						}else {
							entity.setFail(entity.getFail()+1);
						}
					}
				}
				
				String report="";
				if(su>0) {
					report="成功导入"+su+"条记录";
				}
				
				if(fa>0) {
					if("".equals(report)) {
						report="导入"+fa+"条记录失败";
					}else {
						report=report+",导入"+fa+"条记录失败";
					}
				}
				
				entity.setReport(report);
				impService.add(entity);
				
				HashMap<String, Object> result=new HashMap<String, Object>();
				if(success) {
					//result.put("status", true);
					result.put("importstatus", true);
				}else {
					//result.put("status", false);
					result.put("importstatus", false);
				}
				result.put("message", report);
				result.put("status", true);
				return JsonUtils.toJSONString(result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String, Object> result=new HashMap<String, Object>();
		result.put("status", true);
		return JsonUtils.toJSONString(result);
	}

}
